<?php 
Class Ws_Post_Type
{
       private  $type,$type_category,$type_name;
       function __construct($type,$type_category,$type_name,$type_menuicon='dashicons-cart')
       {
              $this->type = $type;
              $this->type_category  = $type_category;
              $this->type_name  = $type_name;
              $this->type_menuicon  = $type_menuicon;
              //添加自定义文章类型
              add_action('init', array($this,'ws_add_type'));
              //为商品类自定义类型增加分类功能
              add_action('init',array($this,'ws_add_categroy'), 0);
              //修改自定义类型文章的地址
              add_filter('post_type_link',array($this,'ws_add_link'), 1, 3);
              //让自定义的url支持伪静态
              add_action('init', array($this,'ws_rewrites_init'));
       }
       //新增自定义文章类型
       public function ws_add_type()
       {
              register_post_type($this->type,
                     array(
                            'labels' => array(
                                   'name' =>$this->type_name,
                                   'title' =>$this->type_name,
                                   'singular_name' => '所有'.$this->type_name,
                                   'add_new' => '添加'.$this->type_name,
                                   'add_new_item' => '添加新'.$this->type_name,
                                   'edit' => '编辑',
                                   'edit_item' => '编辑'.$this->type_name,
                                   'new_item' => '新'.$this->type_name,
                                   'view' => '查看'.$this->type_name,
                                   'view_item' => '查看'.$this->type_name,
                                   'search_items' => '搜索'.$this->type_name,
                                   'not_found' => '没有找到相关'.$this->type_name,
                                   'not_found_in_trash' => '没有'.$this->type_name.'评论',
                                   'parent' =>$this->type_name.'评论',
                            ),
                            'exclude_from_search' => false,
                            'public' => true,
                            'menu_icon' => $this->type_menuicon,
                            'menu_position' => 6,
                            'supports' => array('title', 'editor', 'comments', 'custom-fields', 'thumbnail', 'excerpt', 'page-attributes'),
                            'taxonomies' => array(''),
                            'has_archive' => true,
                            'taxonomies' => array('post_tag'),
                     )
              );
       }
       function ws_add_categroy()
       {
              register_taxonomy(
                     $this->type_category,
                     $this->type,
                     array(
                            'labels' => array(
                                   'name' => $this->type_name.'分类',
                                   'add_new_item' => '添加分类',
                                   'new_item_name' => "新'.$this->type_name.'分类"
                            ),
                            'show_ui' => true,
                            'show_tagcloud' => true,
                            'hierarchical' => true,
                     )
              );
       }
       function ws_add_link($link, $post = 0)
       {
              global $post;
// http://www.99jx.net/?post_type=movie&p=153&preview=true
              if ($post->post_type == $this->type ) {

                     if (get_option('permalink_structure')=='') {
                            return home_url('?post_type='.$this->type . '&p=' . $post->ID );
                     }else{
                            return home_url($this->type . '/' . $post->ID . '.html');
                     }

              }else{
                     return $link;
              }
       }
       function ws_rewrites_init()
       {
              add_rewrite_rule(
                     $this->type . '/([0-9]+)?.html$',
                     'index.php?post_type=' . $this->type . '&p=$matches[1]',
                     'top');
              add_rewrite_rule(
                     $this->type . '/([0-9]+)?.html/comment-page-([0-9]{1,})$',
                     'index.php?post_type=' . $this->type . '&p=$matches[1]&cpage=$matches[2]',
                     'top');

       }
}
 ?>